package com.ruoyi.finance.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.finance.mapper.TVoucherWordMapper;
import com.ruoyi.finance.domain.TVoucherWord;
import com.ruoyi.finance.service.ITVoucherWordService;

/**
 * 凭证字Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-22
 */
@Service
public class TVoucherWordServiceImpl implements ITVoucherWordService 
{
    @Autowired
    private TVoucherWordMapper tVoucherWordMapper;

    /**
     * 查询凭证字
     * 
     * @param id 凭证字主键
     * @return 凭证字
     */
    @Override
    public TVoucherWord selectTVoucherWordById(Long id)
    {
        return tVoucherWordMapper.selectTVoucherWordById(id);
    }

    /**
     * 查询凭证字列表
     * 
     * @param tVoucherWord 凭证字
     * @return 凭证字
     */
    @Override
    public List<TVoucherWord> selectTVoucherWordList(TVoucherWord tVoucherWord)
    {
        return tVoucherWordMapper.selectTVoucherWordList(tVoucherWord);
    }

    /**
     * 新增凭证字
     * 
     * @param tVoucherWord 凭证字
     * @return 结果
     */
    @Override
    public R insertTVoucherWord(TVoucherWord tVoucherWord)
    {
        List<TVoucherWord> tVoucherWords = tVoucherWordMapper.selectList(new QueryWrapper<TVoucherWord>().lambda().eq(TVoucherWord::getName, tVoucherWord.getName()));
        if(tVoucherWords.size()>0){
            return R.fail("凭证字已存在");
        }
        List<TVoucherWord> tVoucherWords2 = tVoucherWordMapper.selectList(new QueryWrapper<TVoucherWord>().lambda().eq(TVoucherWord::getTitle, tVoucherWord.getTitle()));
        if(tVoucherWords2.size()>0){
            return R.fail("凭证字标题已存在");
        }
        if(tVoucherWord.getDefaultType()==1){
            List<TVoucherWord> tVoucherWords1 = tVoucherWordMapper.selectList(new QueryWrapper<TVoucherWord>().lambda().eq(TVoucherWord::getDefaultType, 1));
            if (tVoucherWords1.size() > 0) {
                return R.fail("默认凭证字已存在");
            }
        }
        int insert = tVoucherWordMapper.insert(tVoucherWord);
        if (insert>0) {
            return R.ok();
        }else {
            return R.fail("新增失败");
        }
    }

    /**
     * 修改凭证字
     * 
     * @param tVoucherWord 凭证字
     * @return 结果
     */
    @Override
    public int updateTVoucherWord(TVoucherWord tVoucherWord)
    {
        return tVoucherWordMapper.updateById(tVoucherWord);
    }

    /**
     * 批量删除凭证字
     * 
     * @param ids 需要删除的凭证字主键
     * @return 结果
     */
    @Override
    public R deleteTVoucherWordByIds(Long[] ids)
    {
        for (Long id : ids) {
            if (tVoucherWordMapper.selectById(id).getDefaultType() == 1) {
                return R.fail("默认凭证字不能删除");
            }
        }
        int i = tVoucherWordMapper.deleteTVoucherWordByIds(ids);
        if (i>0) {
            return R.ok();
        }else {
            return R.fail("删除失败");
        }
    }

    /**
     * 删除凭证字信息
     * 
     * @param id 凭证字主键
     * @return 结果
     */
    @Override
    public R deleteTVoucherWordById(Long id)
    {
        if (tVoucherWordMapper.selectById(id).getDefaultType() == 1) {
            return R.fail("默认凭证字不能删除");
        }
        int i = tVoucherWordMapper.deleteTVoucherWordById(id);
        if (i>0) {
            return R.ok();
        }else {
            return R.fail("删除失败");
        }
    }
}
